{extend name="layouts/main" /}
{block name="content"}
<style type="text/css">.childrenBody{background: #fff;}</style>
<table border=0 height=400px align=left>
    <tr>
        <td align=left valign=top>
            <ul class="ztree" style="margin-left: 5px;margin-top:5px; padding: 0;">
                <li><a title="全部展开、折叠"><span class="button ico_open"></span><span id="ztree_expandAll">全部展开、折叠 </span></a></li>
            </ul>
            <ul id="tree" class="ztree" style="overflow:auto;"></ul>
        </td>
    </tr>
</table>
{/block}
{block name="footer"}
{/block}
{block name="js"}
<script type="text/javascript">
layui.use(['jquery', 'ztree'], function() {
    var $ = layui.$;
    var zTree;
    var demoIframe;
    var zNodes = {$json|raw};
    var setting = {
        data: {
            key: {
                name: "catname"
            },
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentid"
            }
        },
        callback: {
            //捕获单击节点之前的事件回调函数
            beforeClick: function(treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return true;
                }
            },
            onClick: function(event, treeId, treeNode) {
                //保存当前点击的栏目ID
                layui.data("tree_catid", {key: 'catid',value: treeNode.id});
            }
        }
    };
    $(document).ready(function() {
        //初始化
        $.fn.zTree.init($("#tree"), setting, zNodes);
        //获取对象
        var zTree = $.fn.zTree.getZTreeObj("tree");
        //全选
        $("#ztree_expandAll").click(function() {
            if ($(this).data("open")) {
                zTree.expandAll(false);
                $(this).data("open", false);
            } else {
                zTree.expandAll(true);
                $(this).data("open", true);
            }
        });
        //定位到上次打开的栏目，进行展开tree_catid
        var tree_catid = layui.data('tree_catid').catid;
        if (tree_catid) {
            var nodes = zTree.getNodesByParam("id", tree_catid, null);
            zTree.selectNode(nodes[0]);
        }
    });
});
</script>
{/block}
